var app=getApp()
var util = require('../../utils/util');
Page({
  data: {
    pickerc:'#acacac',
    array: ['未选择','组队', '问答', '寻物', '跑腿', '二手'],
    objectArray: [
    {
      id: 0,
      name: '未选择'
    },
    {
      id: 1,
      name: '组队'
    },
    {
      id: 2,
      name: '问答'
    },
    {
      id: 3,
      name: '寻物'
    },
    {
      id: 4,
      name: '跑腿'
    },
    {
      id:5,
      name:'二手'
    }
    ],
    delshow:1,
    title:"",
    zone:0,
    content:"",
    reward:"",
    imageid:"",
    imageurl:"",
    pageNum:1,
    fileID:"",
    time:''
  },
  onLoad:function(){

  },
  bindPickerChange: function(e) {
    this.setData({
      zone: e.detail.value
    })
    if(e.detail.value==0){
      this.setData({
        pickerc:'#acacac',
      })
    }
    else{
      this.setData({
        pickerc:'#000000',
      })
    }
  },//选择分区

  showdel:function(e){
    if(this.data.delshow==0){
      this.setData({
        delshow:1
      })
    }
    else{
      this.setData({
        delshow:0
      })
    }
  },//展示删除按钮

  delpic:function(e){
    this.setData({
      imageurl:'',
      imageid:'',
      delshow:1
    })
  },//删除图片

  settitle(e){
    this.setData({
       title:e.detail.value
    })
  },//获取标题

  setcontent(e){
    this.setData({
      content: e.detail.value
   })
  },//设置内容

  setreward(e){
    this.setData({
      reward:e.detail.value
   })
  },//设置报酬

  upData(){
    let that =this
    if(this.data.title==""){
      wx.showToast({
        title: '标题不能为空',
        icon:"none",
        duration: 2000,
        mask: false
       })
    }
    else if(this.data.content==""){
      wx.showToast({
        title: '内容不能为空',
        icon:"none",
        duration: 2000,
        mask: false
       })
    }
    else if(this.data.zone==0)
    {
      wx.showToast({
        title: '分区不能为空',
        icon:"none",
        duration: 2000,
        mask: false
       })
    }
    else
    {
      if(this.data.zone=="1"){
        this.setData({
          zone:"组队"
        })
      }
      else if(this.data.zone=="2"){
        this.setData({
          zone:"问答"
        })
      }
      else if(this.data.zone=="3"){
        this.setData({
          zone:"寻物"
        })
      }
      else if(this.data.zone=="4"){
        this.setData({
          zone:"跑腿"
        })
      }
      else if(this.data.zone=="5"){
        this.setData({
          zone:"二手"
        })
      }
      wx.showLoading({
        title: '发布中...',
        mask:true
      })

    if(this.data.imageurl!='')
    {   
      this.setData({
        imageid:new Date().getTime()+".png"
     })
     this.uploadImg()
    }
    else
    this.go_to_yun()

   }
  },//上传数据
  
go_to_yun(){

  var timestamp = Date.parse(new Date());
  var date = new Date(timestamp);
  //获取年份  
  var Y =date.getFullYear();
  //获取月份  
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  //获取当日日期 
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); 
  var H=date.getHours()<10? '0'+date.getHours():date.getHours()
  var Mi=date.getMinutes()<10? '0'+date.getMinutes():date.getMinutes()
  var myDate={
    'Y':Y,
    'M':M,
    'D':D,
    'H':H,
    'Mi':Mi
  }
  console.log(myDate)
  let that=this
  wx.cloud.callFunction({
    name:"addMessage",
    data:{
     title:this.data.title,
     zone:this.data.zone,
     content:this.data.content,
     reward:this.data.reward,
     PDate:myDate,
     fileID:this.data.fileID,
     username:app.globalData.username,
     userid:app.globalData.openid,
     img:app.globalData.img,
     nDate:date
    },
    success(res){
      console.log("数据上传成功",res)
      wx.hideLoading()
      wx.showToast({
        title: '发布成功！',
        icon: 'success',
        duration: 1500
      })
      that.setData({
        zone:0,
        title:"",
        content:"",
        reward:"",
        imageid:"",
        imageurl:""
      })
      that.onShow()
      // setTimeout(function(){
      //   wx.navigateTo({
      //     url: '../homepage/homepage',
      //   })
      // }, 2000)
    },
    fail(res){
      console.log("数据上传失败",res)
    }
  })
},


  upload(){
    let that=this //这里是为了获取全局的this
  wx.chooseImage({
    count: 1,
    sizeType:['original','compressed'],
    sourceType:['album','camera'],
    success(res){
      console.log("图片选择成功",res)
      that.setData({
        imageurl:res.tempFilePaths[0]
      })
    }
  })
  },//选择图片

  uploadImg(){
    let that=this
    wx.cloud.uploadFile({
      cloudPath:that.data.imageid,
      filePath:that.data.imageurl,
      success(res){

        console.log('文件上传成功',res.fileID)
        that.setData({
          fileID:res.fileID
        })
        that.go_to_yun()
      },
      fail(res){
        console.log('文件上传失败',"Error")
      }
    })
    }//上传图片到云端
})